package com.jumper.cwolf.test;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author cwolf
 * @version 1.0
 * @date 2021/4/26 18:45
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
class HDefinitionDescribe implements Serializable {

    private Long id;

    private String name;


    private Double quantity = 0D;

    public static void main(String[] args) {
        List<HDefinitionDescribe> list1 = new ArrayList<>();

        list1.add(new HDefinitionDescribe(1L,"钙",32.1));
        list1.add(new HDefinitionDescribe(2L,"铁",22.2));
        list1.add(new HDefinitionDescribe(3L,"锌",12.3));


        List<HDefinitionDescribe> list2 = new ArrayList<>();

        list2.add(new HDefinitionDescribe(1L,"钙",33.1));
        list2.add(new HDefinitionDescribe(2L,"铁",22.2));
        list2.add(new HDefinitionDescribe(3L,"锌",11.3));

        List<List<HDefinitionDescribe>> list = new ArrayList<>();
        list.add(list1);
        list.add(list2);

        HDefinitionDescribe hDefinitionDescribe = new HDefinitionDescribe();
        Map<Long, Optional<HDefinitionDescribe>> collect = list.stream().flatMap(a -> a.stream()).collect(Collectors.groupingBy(HDefinitionDescribe::getId, Collectors.reducing((x, y) -> {
            x.setQuantity(y.quantity + x.quantity);
            return x;
        })));
        List<HDefinitionDescribe> newList = collect.values().stream().map(x->x.get()).collect(Collectors.toList());

        System.out.println(JSONUtil.toJsonStr(newList));

    }
}
